<template>
    <view>

    </view>
</template>

<script>
export default {
    data() {
        return {
            openid: '', //openid
            nickname: '', //nickname
            sex: '', //sex
            city: '', //city
            province: '', //province
            country: '', //country
            headimgurl: '', //headimgurl
            reffer_url: '' // 跳转地址
        }
    },
    onLoad() {
        this.openid = this.$route.query.openid
        this.nickname = this.$route.query.nickname
        this.sex = this.$route.query.sex
        this.city = this.$route.query.city
        this.province = this.$route.query.province
        this.country = this.$route.query.country
        this.headimgurl = this.$route.query.headimgurl
        this.reffer_url = this.$route.query.reffer


        // 如果token=1且openid=1 说明是绑定微信，但是微信已经被在使用了，此时无法绑定
        if (this.token == 1 && this.openid == 1) {
            uni.showToast({
                title: '微信已经在使用中，无法被绑定！',
                icon: none,
                mask: true,
                success() {
                    // 把登录信息赋值给 vuex 中的 userinfo
                    setTimeout(function () {
                        uni.navigateTo({
                            url: '/pages/me/me'
                        })
                    }, 2500)

                }
            })

            return
        }

        const _this = this

        uni.showLoading()

        this.$api.tokenLogin({
            openid: this.$route.query.openid,
            nickname: this.$route.query.nickname,
            sex: this.$route.query.sex,
            city: this.$route.query.city,
            province: this.$route.query.province,
            country: this.$route.query.country,
            headimgurl: this.$route.query.headimgurl,
        }).then(res => {

            if (res.code == 1) {
                // 把登录信息赋值给 vuex 中的 userinfo
                _this.$store.dispatch('set_userinfo', res.data)
            } else {
                _this.$store.dispatch('clear_userinfo')
            }

            uni.hideLoading()

			// 微信首次登陆，需要绑定电话号码
			if(res.data['mobile'] == ''){
				uni.navigateTo({
				    url: '/pages/bindtel/bindtel',
				    success: res => {
				    },
				    fail: () => {
				    },
				    complete: () => {
				    }
				});
				return;
			}

            if (_this.reffer_url) {

                // 是否是 tabbar
                if (_this.reffer_url.indexOf('tabbar') >= 0) {
                    uni.switchTab({
                        url: _this.reffer_url
                    })
                } else {
                    uni.navigateTo({
                        url: _this.reffer_url,
                        success: res => {
                        },
                        fail: () => {
                        },
                        complete: () => {
                        }
                    });
                }


            } else {
                uni.switchTab({
                    url: '/pages/tabbar/my/my'
                })
            }


        }).catch(err => {
            uni.switchTab({
                url: '/pages/tabbar/my/my'
            })

            uni.hideLoading()
        })
    },
    methods: {}
}
</script>

<style>

</style>
